package com.cn.demo.controller;

import com.cn.demo.entity.AdminAccounts;
import com.cn.demo.entity.Student;
import com.cn.demo.entity.Teacher;
import com.cn.demo.service.AdminAccountsService;
import com.cn.demo.service.StudentService;
import com.cn.demo.service.TeacherService;
import com.cn.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private AdminAccountsService adminAccountsService;


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,Object> map) {
        String userId = (String)map.get("userId");
        String password = (String)map.get("password");
        String province = (String)map.get("province");
        if("student".equals(province)){
            Student student = new Student();
            student.setStudentId(userId);
            student.setPassword(password);
            return studentService.login(student);
        }

        if("teacher".equals(province)){
            Teacher teacher = new Teacher();
            teacher.setTeacherId(userId);
            teacher.setPassword(password);
            return teacherService.login(teacher);
        }

        if("admin".equals(province)){
            AdminAccounts adminAccounts = new AdminAccounts();
            adminAccounts.setTelephone(userId);
            adminAccounts.setPassword(password);
            return adminAccountsService.login(adminAccounts);
        }
        return Result.error();
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public Result logout(HttpServletRequest request) {
        String province = request.getHeader("province");
        String token = request.getHeader("token");
        if("student".equals(province)){

        }

        if("teacher".equals(province)){

        }

        if("admin".equals(province)){

        }
        return Result.error();
    }


}
